import { createRouter, createWebHistory } from 'vue-router'
import auth from '../utils/auth'

// 路由规则
const routes = [
    {
        path: '/',
        redirect: '/login',
        meta: { requiresAuth: false }
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('../views/Login.vue'),
        meta: { requiresAuth: false }

    },
    {
        path: '/register',
        name: 'Register',
        component: () => import('../views/Register.vue'),
        meta: { requiresAuth: false }

    },
    // {
    //     path: '/home',
    //     name: 'Home',
    //     component: () => import('../views/Home.vue'),
    // },
    {
        path: '/student',
        name: 'Student',
        component: () => import('../views/Student.vue'),
        meta: { title: '首页',requiresAuth: true},
        children: [
            {
                path: 'home',
                name: 'StudentHome',
                component: () => import('../views/Blog.vue'),
                meta: {
                    title: '首页'
                }
            },
            {
                path: 'message',
                name: 'StudentMessage',
                component: () => import('../views/student/Message.vue')
            },
            {
                path: 'class',
                name: 'StudentClass',
                component: () => import('../views/student/Class.vue')
            },
            {
                path: 'chat',
                name: 'StudentChat',
                component: () => import('../views/student/Chat.vue')
            },
            {
                path: 'courseSelection',
                name: 'courseSelection',
                component: () => import('../views/student/CourseSelection.vue')
            },
            {
                path: 'examination',
                name: 'examination',
                component: () => import('../views/student/Examination.vue')
            },
            {
                path: 'grades',
                name: 'grades',
                component: () => import('../views/student/Grades.vue')
            },
        ]
    },
    {
        path: '/teacher',
        name: 'Teacher',
        component: () => import('../views/Teacher.vue'),
        meta: { requiresAuth: true },
        children: [
            {
                path: 'home',
                name: 'TeacherHome',
                component: () => import('../views/Blog.vue')
            },
            {
                path: 'message',
                name: 'TeacherMessage',
                component: () => import('../views/teacher/Message.vue')
            },
            {
                path: 'class',
                name: 'TeacherClass',
                component: () => import('../views/teacher/Class.vue'),
                children: [
                    {
                        path: 'all',
                        name: 'AllCourseSchedule',
                        component: () => import('../views/teacher/class/AllCourseSchedule.vue')
                    },
                    {
                        path: 'type',
                        name: 'CourseType',
                        component: () => import('../views/teacher/class/CourseType.vue')
                    },
                    {
                        path: 'mc',
                        name: 'MyCourse',
                        component: () => import('../views/teacher/class/MyCourse.vue')
                    },
                    {
                        path: 'ms',
                        name: 'MySchedule',
                        component: () => import('../views/teacher/class/MySchedule.vue')
                    }
                ]
            },
            {
                path: 'chat',
                name: 'TeacherChat',
                component: () => import('../views/teacher/Chat.vue')
            }
        ]
    },
    {
        path: '/admin',
        name: 'Admin',
        component: () => import('../views/Admin.vue'),
        meta: { requiresAuth: true },
        children: [
            {
                path:'home',
                name: 'AdminHome',
                component: () => import('../views/Blog.vue')
            },
            {
                path:'college',
                name:'College',
                component: () => import('../views/admin/academic/College.vue')
            },
            {
                path:'major',
                name: 'Major',
                component: () => import('../views/admin/academic/Course.vue')
            },
            {
                path:'academymajor',
                name:'academymajor',
                component: () => import('../views/admin/academic/AcademyMajor.vue')
            },
            {
                path: 'people',
                name: 'people',
                component: () => import('../views/admin/academic/People.vue')
            }
        ]

    },
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
    if (to.meta.requiresAuth && !auth.isLogin()) {
        const token = localStorage.getItem('token')
        if(token){
            next();
        }else{
            next('/login');
        }
    } else{
        next()
    }
});

export default router